package com.woniuxy.enjoylife.service.impl;

import com.woniuxy.enjoylife.entity.Goods;
import com.woniuxy.enjoylife.entity.Goods_times;
import com.woniuxy.enjoylife.entity.dto.ReturnGoodsCommentReplyDto;
import com.woniuxy.enjoylife.entity.dto.ReturnGoodsDto;
import com.woniuxy.enjoylife.entity.dto.SelectGoodsDto;
import com.woniuxy.enjoylife.mapper.GoodsMapper;
import com.woniuxy.enjoylife.mapper.Goods_agreeMapper;
import com.woniuxy.enjoylife.mapper.Goods_timesMapper;
import com.woniuxy.enjoylife.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.enjoylife.utils.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author teamEnjoyLife75
 * @since 2021-01-23
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private Goods_timesMapper goodsTimesMapper;
    @Resource
    private Goods_agreeMapper goodsAgreeMapper;

    //查询商品的方法（分页，分类，用户id，名字模糊，内容模糊,排序）
    @Override
    public PageResult<ReturnGoodsDto> selectGoodsDto(SelectGoodsDto dto) {
//        String content = dto.getContent();
//        dto.setContent(null);
//        if (null != dto.getName() && !dto.getName().equals("")) {
//            dto.setName("%" + dto.getName() + "%");
//        }
//        List<ReturnGoodsDto> returnGoodsDtoList = goodsMapper.selectGoodsDto(dto);
//
//        if (returnGoodsDtoList.size() == 0 && null != dto.getName() && !dto.getName().equals("")) {
//            dto.setName(null).setContent("%" + content + "%");
//            returnGoodsDtoList = goodsMapper.selectGoodsDto(dto);
//        }
//        for (ReturnGoodsDto returnGoodsDto : returnGoodsDtoList) {
//            returnGoodsDto.setOwn(returnGoodsDto.getUid().equals(dto.getSelector()));
//            returnGoodsDto.setIsViewed(goodsTimesMapper.selectIsViewed(returnGoodsDto.getId(), dto.getSelector()));
//            returnGoodsDto.setAgree(goodsAgreeMapper.selectIsAgreed(returnGoodsDto.getId(), dto.getSelector()));
//        }
//        int field = dto.getOrderField();
//        switch (field) {
//            case 0:
//                returnGoodsDtoList.sort(Comparator.comparing(ReturnGoodsDto::getId));
//            case 1:
//                returnGoodsDtoList.sort(Comparator.comparing(ReturnGoodsDto::getType));
//            case 2:
//                returnGoodsDtoList.sort(Comparator.comparing(ReturnGoodsDto::getTimes));
//            case 3:
//                returnGoodsDtoList.sort(Comparator.comparing(ReturnGoodsDto::getPrice));
//            case 4:
//                returnGoodsDtoList.sort(Comparator.comparing(ReturnGoodsDto::getAgreeNum));
//        }
//        if (!dto.getOrderType()) Collections.reverse(returnGoodsDtoList);
//        return new PageResult<ReturnGoodsDto>().setCurrentPage(dto.getPage()).setPageSize(dto.getSize()).setTotalPage(pageInfo.getPageNum()).setTotal(pageInfo.getTotal()).setData(returnGoodsDtoList);
        return pageAllGoodsDto(dto);
    }

    //分页
    public PageResult<ReturnGoodsDto> pageAllGoodsDto(SelectGoodsDto dto) {
        int page = dto.getPage();
        int size = dto.getSize();
        dto.setPage(0);
        dto.setSize(0);
        List<ReturnGoodsDto> returnGoodsDtoList = sortAllGoodsDto(dto);


        PageResult<ReturnGoodsDto> result = new PageResult<>();
        result.setCurrentPage(page);
        result.setPageSize(size);
        result.setTotal(returnGoodsDtoList.size());

        List<ReturnGoodsDto> returnList = new ArrayList<>();

        if (size <= returnGoodsDtoList.size()) {
            if (returnGoodsDtoList.size() % size == 0) {
                result.setTotalPage(returnGoodsDtoList.size() / size);
                for (int i = (page - 1) * size; i < page * size; i++) {
                    returnList.add(returnGoodsDtoList.get(i));
                }
            } else {
                result.setTotalPage(returnGoodsDtoList.size() / size + 1);
                int least = returnGoodsDtoList.size() % size;
                if (page - 1 == 0) {
                    for (int i = 0; i < size; i++) {
                        returnList.add(returnGoodsDtoList.get(i));
                    }
                } else if (page != (returnGoodsDtoList.size() / size + 1)) {
                    for (int i = (page - 1) * size; i < (page - 1) * size; i++) {
                        returnList.add(returnGoodsDtoList.get(i));
                    }
                } else {
                    for (int i = (page - 1) * size; i < (page - 1) * size + least; i++) {
                        returnList.add(returnGoodsDtoList.get(i));
                    }
                }
            }
        } else {
            returnList.addAll(returnGoodsDtoList);
        }
        for (ReturnGoodsDto returnGoodsDto : returnList) {
            returnGoodsDto.setOwn(returnGoodsDto.getUid().equals(dto.getSelector()));
            returnGoodsDto.setIsViewed(goodsTimesMapper.selectIsViewed(returnGoodsDto.getId(), dto.getSelector()));
            returnGoodsDto.setAgree(goodsAgreeMapper.selectIsAgreed(returnGoodsDto.getId(), dto.getSelector()));
            for (ReturnGoodsCommentReplyDto returnGoodsCommentReplyDto : returnGoodsDto.getCommentReplyDtoList()) {
                returnGoodsCommentReplyDto.setMine(returnGoodsCommentReplyDto.getUid().equals(dto.getSelector()));
            }
        }
        result.setData(returnList);
        return result;
    }


    //排序全部
    public List<ReturnGoodsDto> sortAllGoodsDto(SelectGoodsDto dto) {
        int field = dto.getOrderField();
        boolean type = dto.getOrderType();
        dto.setOrderField(0);
        dto.setOrderType(true);
        List<ReturnGoodsDto> returnGoodsDtoList = selectAllGoodsDto(dto);
        switch (field) {
            case 0:
                returnGoodsDtoList.sort(Comparator.comparingInt(ReturnGoodsDto::getId));
                break;
            case 1:
                returnGoodsDtoList.sort(Comparator.comparingInt(ReturnGoodsDto::getType));
                break;
            case 2:
                returnGoodsDtoList.sort(Comparator.comparingInt(ReturnGoodsDto::getTimes));
                break;
            case 3:
                returnGoodsDtoList.sort(Comparator.comparing(ReturnGoodsDto::getPrice));
                break;
            case 4:
                returnGoodsDtoList.sort(Comparator.comparingInt(ReturnGoodsDto::getAgreeNum));
        }
        if (!type) Collections.reverse(returnGoodsDtoList);
        return returnGoodsDtoList;
    }


    //查询全部
    public List<ReturnGoodsDto> selectAllGoodsDto(SelectGoodsDto dto) {
        String content = dto.getContent();
        dto.setContent(null);
        if (null != dto.getName() && !dto.getName().equals("")) {
            dto.setName("%" + dto.getName() + "%");
        }
        List<ReturnGoodsDto> returnGoodsDtoList = goodsMapper.selectGoodsDto(dto);

        if (returnGoodsDtoList.size() == 0 && null != dto.getName() && !dto.getName().equals("")) {
            dto.setName(null).setContent("%" + content + "%");
            returnGoodsDtoList = goodsMapper.selectGoodsDto(dto);
        }

        return returnGoodsDtoList;
    }


    //添加浏览次数
    @Override
    public Boolean updateGoodsTimes(int gid, int uid) {
        if (!goodsTimesMapper.selectIsViewed(gid, uid)) {
            return goodsTimesMapper.insert(new Goods_times().setGid(gid).setUid(uid)) > 0;
        }
        return false;
    }

    //点赞，取消点赞
    @Override
    public Boolean agreeGoods(int gid, int uid) {
        if (goodsAgreeMapper.selectIsAgreed(gid, uid)) {
            return goodsAgreeMapper.deleteAgree(gid, uid);
        }
        return goodsAgreeMapper.insertAgree(gid, uid);
    }

    @Override
    public Boolean deleteGoods(Goods goods) {
        goods.setStatus(1);
        return goodsMapper.delete(goods) >= 1;
    }


}
